package day03;

/**
 * @author Jetwang
 * @version 1.0
 * @since 2023/2/26 15:25
 */
public class BinarySearchRightPlus {
    public static void main(String[] args) {
        int[] arr = { 1, 2, 3, 3, 3, 4, 6, 10, 11 };
        int index = binarySearchRightmost01(arr, 5);
        System.out.println(index);
    }

    /**
     * 返回小于等于目标的最靠右的位置
     * @param arr
     * @param target
     * @return
     */
    public static int binarySearchRightmost01(int[] arr, int target) {
        int low = 0;
        int high = arr.length - 1;
        while (low <= high) {
            int mid = (low + high) >>> 1;
            if (arr[mid] <= target) {
                low = mid + 1;
            } else {
                high = mid - 1;
            }
        }
        return low - 1;
    }
}
